package com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.service;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.bean.BCustomerFreightTicketPricePojo;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.mapper.BCustomerFreightTicketPriceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BCustomerFreightTicketPriceService extends BaseService {

    @Autowired
    private BCustomerFreightTicketPriceMapper bCustomerFreightTicketPriceMapper;


    /**
     * 批量删除起票价,
     * @param agreementFreightIds  格式为 'id1','id2','id3'
     * @return
     */
    public int batchDeleteTicketPriceByAgreementFreightIds(String agreementFreightIds) {
        return bCustomerFreightTicketPriceMapper.batchDeleteTicketPriceByAgreementFreightIds(agreementFreightIds);
    }


    public List queryNoPageList(Map param){
        return bCustomerFreightTicketPriceMapper.queryTicketPriceNoPageList(param);
    }

    /**
     * 无分页
     * @param param
     * @return
     */
    public Object queryTicketPriceNoPageList(Map param) {

        List  list = queryNoPageList(param);

        MIniUIGridResultMap mIniUIGridResultMap = new MIniUIGridResultMap();
        mIniUIGridResultMap.setData(list);
        mIniUIGridResultMap.setTotal(list.size());
        return mIniUIGridResultMap;
    }

    /**
     * 批量保存
     * @param param
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public Object batchSave(Map param) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("批量保存起票价成功!");
        // {freightAgreementId:freightAgreementIdValue,ticketPriceArray:ticketPriceDatagridArray}
        String freightAgreementId = DataTypeUtil.toStringObject(param.get("freightAgreementId"));
        if(freightAgreementId==null){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("协议运价ID丢失，保存失败!");
            return baseResultMap;
        }

        List<Map<String,Object>> ticketPriceMapList = (List<Map<String, Object>>) param.get("ticketPriceArray");
        if(ticketPriceMapList == null || ticketPriceMapList.isEmpty()){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("起票价列表为空，保存失败!");
            return baseResultMap;
        }

        // 设定起票价列表的协议运价 ID
        List<BCustomerFreightTicketPricePojo> ticketPriceList = new ArrayList<>();
        for (Map<String,Object> map : ticketPriceMapList){
            BCustomerFreightTicketPricePojo pojo = new BCustomerFreightTicketPricePojo();
            pojo.setId(getSysStrId());
            pojo.setFreightAgreementId(freightAgreementId);
            pojo.setStandardTypeId(DataTypeUtil.toLongObject(map.get("standardTypeId")));
            pojo.setGoodsTypeId(DataTypeUtil.toLongObject(map.get("goodsTypeId")));
            pojo.setTicketPrice(DataTypeUtil.toDouble(map.get("ticketPrice")));
            pojo.setComment(DataTypeUtil.toStringObject(map.get("comment")));
            ticketPriceList.add(pojo);
        }

        // 根据协议运价ID,删除相应的起票价
        int deleteAffectRows = bCustomerFreightTicketPriceMapper.deleteByFreightAgreementId(freightAgreementId);

        int insertAffectRows = batchInsert(ticketPriceList);

        if(insertAffectRows <= SysConstant.INT_ZERO){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("批量保存起票价失败!");
            return baseResultMap;
        }

        return baseResultMap;
    }

    /**
     * 起票价批量添加
     * @param ticketPricePojos
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public int batchInsert(List<BCustomerFreightTicketPricePojo> ticketPricePojos) throws Exception {
        if(ticketPricePojos ==null || ticketPricePojos.isEmpty()){
            throw new Exception("起票价列表为空，添加失败!");
        }
        return bCustomerFreightTicketPriceMapper.batchInsert(ticketPricePojos);
    }

    @Transactional(rollbackFor = {Exception.class})
    public int deleteByAgreementFreightId(String agreementPojoId) {
        if(agreementPojoId!=null){
            return bCustomerFreightTicketPriceMapper.deleteByFreightAgreementId(agreementPojoId);
        }
        return SysConstant.INT_ZERO;
    }
}
